package com.hailian.fintech.hluserserver.controller;

import com.hailian.fintech.feignserver.feign.AccountFeign;
import com.hailian.fintech.feignserver.feign.TixianFeign;
import com.hailian.fintech.hlbackingserver.entity.Account;
import com.hailian.fintech.hlcomm_interfaceserver.entity.POJO;
import com.hailian.fintech.hlfinanceserver.entity.Jiaoyi;
import com.hailian.fintech.hlfinanceserver.entity.Tixian;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@EnableScheduling
@Configuration
@Slf4j
public class TimeTaskController {

    @Autowired
    private AccountFeign accountFeign;
    @Autowired
    private TixianFeign tixianFeign;

    /**
     * 一天拉取一次用户明细表中的提现数据
     * @return
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public Object cycleAddTixian(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        List<Account> list = accountFeign.selectTiXianAccount("提现");
        list.forEach(account->{
            Tixian tixian = new Tixian();
            tixian.setAccountId(account.getId());
            tixian.setUserId(account.getUid());
            tixian.setSum(account.getPrice());
            try {
                tixian.setCashTime(format.parse(account.getOperation_time()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            tixian.setState(account.getState());
            tixianFeign.addTixian(tixian);
        });

        return true;
    }

    /**
     * 一天拉取一次（用户明细表，收房账单，租房账单，收,付款计划表）
     * 中的所有已完成状态数据到交易流水表
     * @return
     */
    @Scheduled(cron = "0/7 * * * * ?")
    public Object cycleAddJiaoYi(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //把明细表中交易完成数据得到
        List<Account> list = accountFeign.selectToAddJiaoYi("审核通过");
        System.err.println(list);
        list.forEach(account->{
            Jiaoyi jiaoyi = new Jiaoyi();
            jiaoyi.setYingyeId("YH-"+ UUID.randomUUID().toString());
            jiaoyi.setPrice(account.getPrice());
            jiaoyi.setBank_id(new Random().nextInt(5));
            try {
                jiaoyi.setCreated(format.parse(account.getOperation_time()));
                jiaoyi.setDoing_time(format.parse(account.getOperation_time()));
                jiaoyi.setEnd_time(format.parse(account.getOperation_time()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            tixianFeign.addJiaoyi(jiaoyi);
        });

        //把账单表中交易完成数据得到
        //把账单表中交易完成数据得到
        //把收款付款计划表表中交易完成数据得到

        return true;
    }


}
